<template>
	<view class="wrap">
		<view class="top"></view>
		<view class="content">
			<view class="title">欢迎登录悦彩APP</view>
			<input class="u-border-bottom" type="number" v-model="tel" placeholder="请输入手机号" />
			<view class="tips">手机号即是用户账号</view>
			<input class="u-border-bottom" type="password" v-model="password" placeholder="请输入密码" />
			<view class="tips">账号登录密码</view>
			<view class="imgCode">
				<input class="u-border-bottom" v-model="code" placeholder="请填写验证码" />
				<view class="" @tap="getImgCode()"><image style="height: 60rpx;width: 230rpx;" :src="imgCode"></image></view>
			</view>
			<button @tap="submit" type="success" class="login">立即登录</button>
			<view class="alternative">
				<view @tap="goIndex" class="password">手机号登录</view>
				<!-- <view class="issue">遇到问题</view> -->
			</view>
		</view>
		<view class="buttom">
			<view class="hint">
				登录代表同意
				<text class="link">用户协议、隐私政策，</text>
				并授权使用您的账号信息（如昵称、头像、收获地址）以便您统一管理
			</view>
		</view>
		<view><u-keyboard ref="uKeyboard" mode="number" @change="valChange" @backspace="backspace" v-model="keyboardShow"></u-keyboard></view>
	</view>
</template>

<script>
import { encrypt } from '@/common/rsaEncrypt.js';

export default {
	data() {
		return {
			tel: '',
			password: '',
			code: '', //验证码
			imgCode: '',
			uuid: '',
			//
			index: 0,
			value: '',
			keyboardShow: false
		};
	},
	computed: {
		inputStyle() {
			let style = {};
			if (this.tel) {
				style.color = '#fff';
				style.backgroundColor = this.$u.color['warning'];
			}
			return style;
		}
	},
	onLoad() {
		try {
			const token = uni.getStorageSync('token');
			console.log(token);
			const userInfo = uni.getStorageSync('userInfo');
			if (token) {
				this.goCenter();
			} else {
				uni.removeStorageSync('token');
				uni.removeStorageSync('userInfo');
			}
		} catch (e) {
			// error
		}
		this.getImgCode();
	},
	methods: {
		//
		openKeyboard(index) {
			let _t = this;
			_t.keyboardShow = true;
			_t.index = index;
		},
		// 按键被点击(点击退格键不会触发此事件)
		valChange(val) {
			// 将每次按键的值拼接到value变量中，注意+=写法
			let _t = this;
			if (_t.index == 1) {
				_t.tel += val;
			}
			if (_t.index == 2) {
				_t.code += val;
			}
		},
		// 退格键被点击
		backspace() {
			// 删除value的最后一个字符
			let _t = this;
			if (_t.index == 1) {
				if (_t.tel.length) _t.tel = _t.tel.substr(0, _t.tel.length - 1);
			}
			if (_t.index == 2) {
				if (_t.code.length) _t.code = _t.code.substr(0, _t.code.length - 1);
			}
		},
		//
		goIndex() {
			this.$u.route('/pages/login/index');
		},
		goCenter() {
			uni.switchTab({
				url: '/pages/center/index'
			});
		},
		//登录
		submit() {
			if (this.$u.test.mobile(this.tel)) {
				this.password = encrypt(this.password);
				console.log(this.password);
				this.$u.api.postUserLogin({ phone: this.tel, code: this.code, password: this.password, uuid: this.uuid }).then(res => {
					console.log(res.data.token);
					try {
						uni.setStorageSync('token', res.data.token);
						uni.setStorageSync('userInfo', res.data.user.user);
						this.goCenter();
					} catch (e) {
						// error
					}
				});
				/* this.$u.route({
					url: 'pages/template/login/code'
				}); */
			} else {
				this.$u.toast('手机号码格式错误');
			}
		},
		getImgCode() {
			this.$u.api.getImgCode().then(res => {
				if (res) {
					this.imgCode = res.data.img;
					this.uuid = res.data.uuid;
					try {
						uni.setStorageSync('img_code', imgCode);
						uni.setStorageSync('uuid', uuid);
					} catch (e) {
						// error
					}
				} else {
					this.$u.toast('获取验证码失败');
				}
			});
		}
	}
};
</script>

<style lang="scss" scoped>
.wrap {
	font-size: 28rpx;
	.content {
		width: 600rpx;
		margin: 80rpx auto 0;

		.title {
			text-align: left;
			font-size: 60rpx;
			font-weight: 500;
			margin-bottom: 100rpx;
		}
		input {
			text-align: left;
			margin-bottom: 10rpx;
			padding-bottom: 6rpx;
		}
		.imgCode {
			display: flex;
			align-items: center;
		}
		.tips {
			color: $u-type-info;
			margin-bottom: 60rpx;
			margin-top: 8rpx;
		}
		.login {
			background-color: #19be6b;
			color: #f4f4f5;
			border: none;
			font-size: 30rpx;
			padding: 12rpx 0;
			margin: 20rpx 0;

			&::after {
				border: none;
			}
		}

		.getCaptcha {
			background-color: rgb(253, 243, 208);
			color: $u-tips-color;
			border: none;
			font-size: 30rpx;
			padding: 12rpx 0;
			margin: 20rpx 0;

			&::after {
				border: none;
			}
		}
		.alternative {
			color: $u-tips-color;
			display: flex;
			justify-content: space-between;
			margin-top: 30rpx;
		}
	}
	.buttom {
		.loginType {
			display: flex;
			padding: 350rpx 150rpx 150rpx 150rpx;
			justify-content: space-between;

			.item {
				display: flex;
				flex-direction: column;
				align-items: center;
				color: $u-content-color;
				font-size: 28rpx;
			}
		}

		.hint {
			padding: 40rpx 40rpx;
			font-size: 20rpx;
			color: $u-tips-color;

			.link {
				color: $u-type-warning;
			}
		}
	}
}
</style>
